﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Model;

namespace Joker.View.Editor {
    public partial class UserManageEdit : Form {
        public int ID = -1;
        public UserManageEdit() {
            InitializeComponent();
        }

        private void UserManageEdit_Load(object sender, EventArgs e) {
            if (ID != -1) {
                DataTable tmp = DBHelper.GetDataTable($"SELECT * FROM [User] WHERE [ID] = {ID}");
                textBox1.Text = tmp.Rows[0][0].ToString();
                textBox2.Text = TextData.Debase64(tmp.Rows[0][1].ToString());
                textBox4.Text = tmp.Rows[0][3].ToString();
                comboBox1.SelectedIndex = tmp.Rows[0][4].ToString().Equals("1") ? 0 : 1;
                textBox5.Text = tmp.Rows[0][5].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e) {
            Close();
        }

        private void button2_Click(object sender, EventArgs e) {
            if (textBox2.Text.Length * textBox3.Text.Length * textBox4.Text.Length * textBox5.Text.Length == 0) {
                MessageBox.Show("内容不得为空!");
                return;
            }
            if (ID == -1) {
                if (DBHelper.ExecuteNonQuery($"insert [User] values('{TextData.Base64(textBox2.Text)}','{TextData.MD5(textBox3.Text)}','{textBox4.Text}',{(comboBox1.SelectedItem.Equals("男") ? 1 : 0)},'{textBox5.Text}')")) {
                    MessageBox.Show("添加成功!");
                    Close();
                } else {
                    MessageBox.Show("添加失败!");
                }
            } else {
                if (DBHelper.ExecuteNonQuery($"update [User] set [Account] = '{TextData.Base64(textBox2.Text)}',[Password]='{TextData.MD5(textBox3.Text)}',[Name]='{textBox4.Text}',[Gender]={(comboBox1.SelectedItem.Equals("男") ? 1 : 0)},[Tel]='{textBox5.Text}' where ID = {textBox1.Text}")) {
                    MessageBox.Show("修改成功!");
                    Close();
                } else {
                    MessageBox.Show("修改失败!");
                }
            }
        }
    }
}
